package com.zkingsoft.actions.admin.lin;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.interceptors.DuplicateSubmitInterceptor;
import com.zkingsoft.model.sys.SysUsers;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.sys.SysUsersService;
import com.zkingsoft.util.WebUtil;

@Controller
@RequestMapping(value = "admin/linAccount")
public class LinAccountController extends BaseController {

	@Resource
	private SysUsersService sysUsersService;

	@Resource
	private MatrixAdapter matrixAdapter;

	// 记录编辑前的值Before_Edit_Value
	public static final String factoryBEV = "factory_BEV";
	public static final String hotelBEV = "hotel_BEV";

	public static final String factoryFnCode = "factoryAccountManage";
	public static final String hotelFnCode = "hotelAccountManage";

	public static final String search = ":search";
	public static final String edit = ":edit";
	public static final String del = ":del";
	public static final String add = ":add";

	/**
	 * 列表显示工厂帐号
	 */
	@RequestMapping(value = "/showFactoryList")
	@ResponseBody
	public AjaxResult showFactoryList(SysUsers sysUsers, PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(factoryFnCode + search);
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		sysUsers.setRolePlates(SystemConstance.PLAT_FACTORY);
		List<SysUsers> dataList = sysUsersService.findInPage(sysUsers, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null,
				dataList, sysUsersService.findTotal(sysUsers));
		return result;

	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editFactoryForm")
	public String editFactoryForm(Long id) {
		matrixAdapter.isBtnPermitted(factoryFnCode + edit);
		SysUsers sysUsers;
		if (id != null) {
			DuplicateSubmitInterceptor.pushFormName("suId", "suName",
					"roleIds", "suAccount", "suTel", "suEmail", "plate");
			sysUsers = sysUsersService.findById(id);
			WebUtil.getRequest().setAttribute("obj", sysUsers);
			WebUtil.setSessionAttribute(factoryBEV, sysUsers);
		} else {
			DuplicateSubmitInterceptor.pushFormName("suName", "roleIds",
					"suAccount", "suTel", "suEmail", "suPassword", "plate");
		}
		WebUtil.getRequest()
				.setAttribute("plate", SystemConstance.PLAT_FACTORY);

		return "admin/lin/Account-form";
	}

	/**
	 * 新增或者修改帐号
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModifyAccount")
	@ResponseBody
	public AjaxResult addOrModifyAccount(SysUsers sysUsers, String plate) {

		String fnCode = "";
		String BEV = "";
		switch (plate) {
		case SystemConstance.PLAT_FACTORY:
			fnCode = factoryFnCode;
			BEV = factoryBEV;
			break;
		case SystemConstance.PLAT_HOTEL:
			fnCode = hotelFnCode;
			BEV = hotelBEV;
			break;
		default : return new AjaxResult(AjaxResult.STATUS_ERR, "帐号类型错误");
		}

		if (sysUsers.getSuId() != null) {
			matrixAdapter.isBtnPermitted(fnCode + edit);
			// 重新设置修改对象的id
			sysUsers.setSuId(((SysUsers) WebUtil
					.getSessionAttribute(BEV)).getSuId());
			AjaxResult result = modify(sysUsersService,
					WebUtil.getSessionAttribute(BEV), sysUsers, "帐号");
			WebUtil.removeSessionAttribute(BEV);
			return result;
		} else {
			matrixAdapter.isBtnPermitted(fnCode + add);
			// 设置一些基本信息
			sysUsers.setSuAccountStatus(SystemConstance.ACCOUNT_STATUS_ACTIVATE);
			sysUsers.setSuRegisterTime(new Date());
			sysUsers.setSuValid(SystemConstance.RECORD_VALID);
			// 设置账号的类型为公司员工
			sysUsers.setSuUserType(SystemConstance.USER_TYPE_EMPLOYEE);
			SysUsers user = matrixAdapter.getLoginUser();
			sysUsers.setCompanyId(user.getCompanyId());

			return add(sysUsersService, sysUsers, "帐号");
		}
	}

	/**
	 * 列表显示酒店帐号
	 */
	@RequestMapping(value = "/showHotelList")
	@ResponseBody
	public AjaxResult showHotelList(SysUsers sysUsers, PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(hotelFnCode + search);
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		sysUsers.setRolePlates(SystemConstance.PLAT_HOTEL);
		List<SysUsers> dataList = sysUsersService.findInPage(sysUsers, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null,
				dataList, sysUsersService.findTotal(sysUsers));
		return result;

	}

	/**
	 * 进入修改界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editHotelForm")
	public String editHotelForm(Long id) {
		matrixAdapter.isBtnPermitted(hotelFnCode + edit);
		SysUsers sysUsers;
		if (id != null) {
			DuplicateSubmitInterceptor.pushFormName("suId", "suName",
					"roleIds", "suAccount", "suTel", "suEmail", "plate", "suDeptId");
			sysUsers = sysUsersService.findById(id);
			WebUtil.getRequest().setAttribute("obj", sysUsers);
			WebUtil.setSessionAttribute(hotelBEV, sysUsers);
		} else {
			DuplicateSubmitInterceptor.pushFormName("suName", "roleIds",
					"suAccount", "suTel", "suEmail", "suPassword", "plate", "suDeptId");
		}
		WebUtil.getRequest().setAttribute("plate", SystemConstance.PLAT_HOTEL);

		return "admin/lin/Account-form";
	}

}
